<?php

class Admin_CursofaculdadeController extends App_Controller_Action
{
    private $_table     = 'CURSOFACULDADE';
    private $_pk        = 'CUF_ID';
    private $_order_col = 'CUF_NOM';
    private $_order     = 'ASC';
    private $_sta_col   = 'CUF_STA';
    private $obj        = '';
    private $form       = '';
    
    public function init()
    {
        $this->obj = new Admin_Model_DbTable_Cursofaculdade();
        if($this->getRequest()->getActionName() == 'edit') $this->form = new Admin_Form_Cursofaculdade();
    }

    public function indexAction() {
        Zend_Loader::loadClass('tools','library');
        Zend_Loader::loadClass('elemform','library');
        
        $where = $this->_pk . ' > 0';
        
        //$filtro = $this->_getParam('itfiltro', '1'); 
        //if($filtro != '') $where .= ' and MTL_STA = ' . $filtro;
        //$this->view->filtro = $filtro;
        
        //$STA = $this->_getParam($this->_sta_col, 1);
        //$where .= ' and ' . $this->_sta_col . ' = ' . $STA;
        
        $result = $this->obj->fetchAll($this->obj->select()->where($where)->order($this->_order_col.' '.$this->_order));
        $limit = 20;
        $this->view->limit = $limit / 2;
        $this->view->result = $this->paginacao($result,$limit);
    }
    
    public function editAction() {
        
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/validation/dist/jquery.validate.min.js');
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/view/s_form_pdr.js');
        
        $this->view->form = $this->form;
        if($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            try {
                if(1){//$this->form->isValid($formData)) {
                    $id = (int) $formData[$this->_pk];//$this->form->getValue($this->_pk);
                    
                    $data = $formData;//$this->form->getValues();
                    unset($data['submit']);
                    
                    if ($id == 0) $this->obj->insert($data);
                    else $this->obj->update($data, $this->_pk.' = ' . $id);
                    
                    $this->_helper->redirector('index');
                } else {
                    //$this->form->populate($formData);
                    $this->view->data = $formData;
                }
            } catch(Exception $ex) {
                Zend_Loader::loadClass('errordb','library');
                echo '<script>alert("'.$ex->getMessage().'");</script>';
                // echo $ex->getMessage();
                //$this->form->populate($formData);
                $this->view->data = $formData;
            }
        } else {
            $id = $this->_getParam('id', 0);
            if ($id > 0) {
                $data = $this->obj->fetchRow($this->_pk . ' = ' . $id)->toArray();
                //$this->form->populate($formData);
                $this->view->data = $data;
            }
        }
    }

    public function deleteAction() {
        $this->_helper->layout->disableLayout(); 
        $this->_helper->viewRenderer->setNoRender();  
        $ok = FALSE;
        $id = $this->_getParam('id', 0);
        if ((int)$id > 0 ) {   
            //$data = array($this->_sta_col => 0);
            //$ok = (bool) $this->obj->update($data, $this->_pk . ' = ' . $id);
            $ok = (bool) $this->obj->delete($this->_pk . ' = ' . $id);
        }       
        echo json_encode(array('ok' => $ok));
    }
    
}
